package com.test.data.string;

import java.io.UnsupportedEncodingException;


/**
 * 
 * 常用方法，便于以后各工程共用！
 * lxf
 * 
 * 
 * 相关：
 * {@link TextUtils#htmlEncode(String)} 
 * {@link TextUtils#htmlEncode(String)} 

 * 
 */
public class StrUtil {

	
	public static final String TAG = "StrUtil";
	public static final String CHARSET_GBK = "GBK";
	
	/**
	 * byte 转无符号整数
	 * 
	 * @param by
	 *@return
	 */
	public static int byteToUnsignedInt(byte by) {
		return (int) (by & 0xFF);
	}




	/**
	 * 
	 *@param by
	 *@param tbegin
	 *@param tend
	 *@return
	 */
	public static byte[] subByte(byte[] by, int tbegin, int tend) {
		byte[] res = new byte[tend - tbegin];

		for (int i = 0; i < res.length; i++) {
			res[i] = by[tbegin + i];
		}
		return res;
	}

	/**
	 * 截取byte获得子字符串！！
	 * 
	 *@param by
	 *@param tbegin
	 *@param tEnd
	 *@return
	 */
	public static String subByString(byte[] by, int tbegin, int tEnd) {
		if ((tEnd - tbegin) == 0) {
			return "";
		}
		byte[] tByte = subByte(by, tbegin, tEnd);

		return byteToStr(tByte);
	}

	/**
	
	/**
	 * 追加byte数组（将toAdd全部追加到目标数组）
	 * 
	 *@param destByte
	 *@param begin
	 *@param toAdd
	 *@return
	 */
	public static byte[] appendBytes(byte[] destByte, int begin, byte[] toAdd) {
		int i = begin;

		for (int index = 0; index < toAdd.length; index++) {
			destByte[i + index] = toAdd[index];
		}
		return destByte;
	}

	/**
	 * 追加byte数组（toAdd追加的范围0~len-1）
	 * 
	 *@param destByte
	 *@param detBegin
	 *@param toAdd
	 *            实际长度len
	 * 
	 *@return
	 */
	public static byte[] AppendBytes(byte[] destByte, int detBegin, int len,
			byte[] toAdd) {

		System.arraycopy(toAdd, 0, destByte, detBegin, len);
		return destByte;
	}

	

	/**
	 * 获得实际长度的byte
	 * 
	 * @param len 实际长度 
	 * 
	 *@return
	 */
	public static byte[] getTrueLenByte(byte[] srcBuf, int len) {
		if (srcBuf.length < len) {
			// error
		}

		byte[] res = new byte[len];
		System.arraycopy(srcBuf, 0, res, 0, len);

		return res;

	}

	/**
	 * byte 全转String
	 *  * 如果数组是空，返回null ;
	 *  
	 * @param bArray
	 *@return null or str
	 */
	public static String byteToStr(byte[] bArray) {
		if(bArray == null){
			return null ;
		}
		
		/////
		try {
			return new String(bArray, CHARSET_GBK);

		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	
	/**
	 * 如果数组是空，返回空字符串！
	 * 
	 *@param bArray
	 *@return
	 */
	public static String byteToStr2(byte[] bArray) {
		if(bArray == null){
			return "" ;
		}
		
		/////
		try {
			return new String(bArray, CHARSET_GBK);

		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	
	/**
	 * byteToStr
	 * 
	 * @param bArray
	 *@param trueLen
	 *            实际长度
	 * 
	 *@return
	 */
	public static String byteToStr(byte[] bArray, int trueLen) {
		try {
			return new String(bArray, 0, trueLen, CHARSET_GBK);

		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static byte[] strToByte(String str) {
		try {
			return str.getBytes(CHARSET_GBK);

		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		}
	}




	/**
	 * 字符串是否为空
	 *@param str
	 *@return true == 为空
	 *
	 */
	public static boolean isEmpty(CharSequence str)
	{
	    if (str == null || str.length() == 0)
	        return true;
	    else
	        return false;
	}

	/**
	 * 数组为空
	 *@param array
	 *@return
	 */
	public static  <T extends Object> boolean isEmptyArr(T[] array)
	{
		if(array == null || array.length == 0){
			return true ;
		}else{
			return false ;
		}
	}

	
	
	


}
